﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Data;
using BusinessObjects.BO_GetObjects;
using System.Xml;
using DigiLib;
using System.Text;
using System.IO;

public partial class Pages_Index : System.Web.UI.Page
{
    private int category_id;
    private DataTable objDT;
    protected void Page_Load(object sender, EventArgs e)
    {
        form1.Action = Request.RawUrl;
        Setting.statistic(HttpContext.Current);
        category_id = Setting.getRequest(Request, "category_id", 72);
        LoadModule(1, plhdTop);
        LoadModule(2, plhdLeft);
        LoadModule(3, plhdCenter);
        LoadModule(4, plhdRight);
        //LoadModule(5, plhdBottom);
        if (!IsPostBack)
        {
            //loadMenu();
            loadFooter();
            category_id = Setting.getRequest(Request, "category", category_id);
            string script = "$(\"#menu ul li a#c" + category_id.ToString() + "\").addClass('active');";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Information", script, true);
            //int depth = Convert.ToInt32(BO_portal_categories.Depth(category_id, 0));
            //string smap = string.Empty;
            //while (depth > 0)
            //{
            //    depth--;
            //    objDT = BO_portal_categories.SelectParentId(category_id, depth);
            //    if (string.IsNullOrEmpty(smap))
            //    {
            //        //ctTitle.InnerText = objDT.Rows[0]["FULL_CATEGORY_NAME"].ToString();
            //        smap = "<a href='/' class='home' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>  » ";
            //    }
            //    if (depth > 0)
            //    {
            //        smap += "<a href='/c" + objDT.Rows[0]["CATEGORY_ID"].ToString() + "/" + StringUtils.RemoveUnicode(objDT.Rows[0]["FULL_CATEGORY_NAME"].ToString(), "-") + ".htm'>" + objDT.Rows[0]["FULL_CATEGORY_NAME"].ToString() + "</a> » ";
            //    }
            //    else
            //    {
            //        smap += "<a href='/c" + objDT.Rows[0]["CATEGORY_ID"].ToString() + "/" + StringUtils.RemoveUnicode(objDT.Rows[0]["FULL_CATEGORY_NAME"].ToString(), "-") + ".htm' class='active'>" + objDT.Rows[0]["FULL_CATEGORY_NAME"].ToString() + "</a>";
            //        if (Request["article_id"] == null)
            objDT = BO_portal_categories.Search(category_id);
            if (objDT.Rows.Count > 0)
                Setting.SetMetaTags(objDT.Rows[0]["FULL_CATEGORY_NAME"].ToString() + " - " + DigiLib.PortalConfig.GetPageTitle, this.Page, objDT.Rows[0]["FULL_CATEGORY_NAME"].ToString() + " - " + DigiLib.PortalConfig.GetMetaKeywords, objDT.Rows[0]["FULL_CATEGORY_DESCRIPTION"].ToString() + " - " + DigiLib.PortalConfig.GetMetaDescription);
            //    }
            //}
            //sitemap.InnerHtml = smap;
        }
    }

    protected void LoadModule(int location_id, PlaceHolder _PlaceHolder)
    {
        try
        {
            _PlaceHolder.Controls.Clear();
            //string cache_key = "PORTAL_MODULES_SELECT_HOME_" + category_id.ToString() + location_id.ToString();
            //if (Cache[cache_key] != null) {
            //    objDT = (DataTable)Cache[cache_key];
            //} else {
            objDT = BO_portal_modules.SelectHome(category_id, location_id);
            //    Cache[cache_key] = objDT;
            //}

            if (objDT != null && objDT.Rows.Count > 0)
            {
                foreach (DataRow row in objDT.Rows)
                {
                    UserControl control = (UserControl)Page.LoadControl("~/Controls/" + row["TYPE_NAME"].ToString() + ".ascx");
                    control.Attributes.Add("ID", row["MODULE_ID"].ToString());
                    _PlaceHolder.Controls.Add(control);
                }
            }
        }
        catch
        {
            throw;
        }
    }
    protected void loadFooter()
    {
        try {
            string fname = Server.MapPath(Setting.PathFooter);
            if(!File.Exists(fname)) {
                return;
            }
            XmlDocument doc = new XmlDocument();
            doc.Load(fname);
            XmlNode root = doc.DocumentElement;
            XmlNodeList list = root.ChildNodes;
            footer.InnerHtml = Server.HtmlDecode(list.Item(0).InnerText);
        } catch(Exception ex) {

        }

    }

    protected void loadMenu()
    {
        //DataTable objDT = BO_portal_categories.SelectByParent(0, 1);
        //rptHomeMenu.DataSource = objDT;
        //rptHomeMenu.DataBind();
    }
    protected void lbtnEN_Click(object sender, EventArgs e)
    {
        Session["lang"] = "en-US";
        Response.Redirect(Request.RawUrl, false);
    }
    protected void lbtnVN_Click(object sender, EventArgs e)
    {
        Session["lang"] = null;
        Response.Redirect(Request.RawUrl, false);
    }
}